#!coding=utf-8
from chat.models import ChatCont, Roomsta
from django.core.cache import cache
from django.http.response import HttpResponse
import json
import datetime
from shop.settings import MEDIA_ROOT_CHAT
from django.core.exceptions import ObjectDoesNotExist

def postmsg(request): 
#     print "postinfo" 
#     {"chatname":"openidid1242sadfj23241;123","content":"1133221","sender":"openidid1242sadfj23241","receiver"="123"}
    
    if request.body == "":
        return HttpResponse("error")
    
    received_json_data = json.loads(request.body)
    
    chatname=received_json_data["chatname"];
    sender=received_json_data["sender"];
    receiver=received_json_data["receiver"];
    content=received_json_data["content"];
    msgtype=received_json_data["msgtype"];
    filename="";
    try:
        tmproomsta=Roomsta.objects.get(roomname=chatname)
        tmproomsta.roomsts='1'
        tmproomsta.save()
    except ObjectDoesNotExist:
        roomsta=Roomsta(roomname=chatname,sender=sender,receiver=receiver,roomsts='1')
        roomsta.save()
    if msgtype=='3':
        filename=MEDIA_ROOT_CHAT+str(datetime.datetime.now())
        f=open(filename,'w')
        f.write(content)
        f.close()
        tmpcont=ChatCont(chatname=chatname,sender=sender,msgsts='0',receiver=receiver,content=filename,msgtype=msgtype)
        tmpcont.save();
    else:
        tmpcont=ChatCont(chatname=chatname,sender=sender,msgsts='0',receiver=receiver,content=content,msgtype=msgtype)
        tmpcont.save();
#     cache.delete(chatname);
#     cache.set(chatname,tmpcont.id,6000);
    
    return HttpResponse(json.dumps({"id":tmpcont.id}));

def getmsg(request): 
#     {"chatname":"openidid1242sadfj23241;123","maxid":"121"}
    resp={}
    if request.body == "":
        return (json.dumps({}));
    received_json_data = json.loads(request.body)
    chatname=received_json_data["chatname"];
    maxid=received_json_data["maxid"];
    
    chatcont=ChatCont.objects.filter(chatname=chatname,id__gt=int(maxid)).order_by('-modify_at')[:100];
    if len(chatcont):
        rmaxid=chatcont[0].id
        for tmpcont in chatcont:
            tmpresp={}
            tmpresp["sender"]=tmpcont.sender;
            tmpresp["receiver"]=tmpcont.receiver;
            tmpresp["content"]=tmpcont.content;
            tmpresp["msgtype"]=tmpcont.msgtype;
            tmpresp["msgsts"]=tmpcont.msgsts;
            tmpresp["time"]=str(tmpcont.modify_at);
            resp[tmpcont.id]=tmpresp
        return HttpResponse(json.dumps(resp));
    else:
        return HttpResponse(json.dumps({}));
    
def servergetmsg(request): 
#     {"chatname":"openidid1242sadfj23241;123","maxid":"121"}
    resp={}
    if request.body == "":
        return (json.dumps({}));
    received_json_data = json.loads(request.body)
    chatname=received_json_data["chatname"];
    maxid=received_json_data["maxid"];
    chatcont=ChatCont.objects.filter(chatname=chatname,id__gt=int(maxid),msgsts='0').order_by('-modify_at')[:100];
    if len(chatcont):
        for tmpcont in chatcont:
            tmpresp={}
            tmpresp["sender"]=tmpcont.sender;
            tmpresp["receiver"]=tmpcont.receiver;
            tmpresp["content"]=tmpcont.content;
            tmpresp["msgtype"]=tmpcont.msgtype;
            tmpresp["msgsts"]=tmpcont.msgsts;
            tmpresp["time"]=str(tmpcont.modify_at);
            resp[tmpcont.id]=tmpresp
        ChatCont.objects.filter(chatname=chatname,id__gt=int(maxid),msgsts='0').update(msgsts='1')
        return HttpResponse(json.dumps(resp));
    else:
        return HttpResponse(json.dumps({}));
        